{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy import signal as sg\n",
    "from time import sleep\n",
    "from PIL import Image\n",
    "import numpy as np\n",
    "import string\n",
    "import shutil\n",
    "import base64\n",
    "import cv2\n",
    "import re\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.imread('3.png',0)\n",
    "th = 200\n",
    "img = 255-(img>th)*255\n",
    "cv2.imwrite('3.png', img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  0   0   0 ...   0   0   0]\n",
      " [  0   0   0 ...   0   0   0]\n",
      " [  0   0   0 ... 255   0   0]\n",
      " ...\n",
      " [255 255 255 ... 255   0   0]\n",
      " [  0   0   0 ...   0   0   0]\n",
      " [  0   0   0 ...   0   0   0]]\n",
      "35\n"
     ]
    }
   ],
   "source": [
    "img = cv2.imread('3.3.png',0)\n",
    "#th = 200\n",
    "#img = 255-(img>th)*255\n",
    "#cv2.imwrite('3.3.png', img)\n",
    "w=img.shape[1]\n",
    "print(img)\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "18\n",
      "19\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "7\n",
      "11\n",
      "15\n",
      "16\n",
      "22\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img = cv2.imread('3.3.png',0)\n",
    "\n",
    "i = 0\n",
    "while i + 3 <= img.shape[1]:\n",
    "    flag_skip=False\n",
    "    imgbox = img[:,i:i+3]\n",
    "    for arr in imgbox:\n",
    "        # 相同的色块合并3，然后白的（内容）并2\n",
    "        if sum(arr) % 765 != 0:\n",
    "            i += 1\n",
    "            flag_skip=True\n",
    "            break\n",
    "    if flag_skip == False:\n",
    "        #print(img.shape[1])\n",
    "        #print(i)\n",
    "        img=np.delete(img, i, 1)\n",
    "        \n",
    "i = 0\n",
    "while i + 3 <= img.shape[0]:\n",
    "    flag_skip=False\n",
    "    imgbox = img[i:i+3,:].transpose()\n",
    "    for arr in imgbox:\n",
    "        # 相同的色块合并3，然后白的（内容）并2\n",
    "        if sum(arr) % 765 != 0:\n",
    "            i += 1\n",
    "            flag_skip=True\n",
    "            break\n",
    "    if flag_skip == False:\n",
    "        #print(img.shape[0])\n",
    "        #print(i)\n",
    "        img=np.delete(img, i, 0)\n",
    "\n",
    "i = 0\n",
    "while i + 2 <= img.shape[1]:\n",
    "    flag_skip=False\n",
    "    imgbox = img[:,i:i+2]\n",
    "    for arr in imgbox:\n",
    "        # 相同的色块合并3，然后白的（内容）并2\n",
    "        if sum(arr) % 510 != 0:\n",
    "            i += 1\n",
    "            flag_skip=True\n",
    "            break\n",
    "    if flag_skip == False:\n",
    "        #print(img.shape[1])\n",
    "        #print(i)\n",
    "        img=np.delete(img, i, 1)\n",
    "\n",
    "i = 0\n",
    "while i + 2 <= img.shape[0]:\n",
    "    flag_skip=False\n",
    "    imgbox = img[i:i+2,:].transpose()\n",
    "    for arr in imgbox:\n",
    "        # 相同的色块合并3，然后白的（内容）并2\n",
    "        if sum(arr) % 510 != 0:\n",
    "            i += 1\n",
    "            flag_skip=True\n",
    "            break\n",
    "    if flag_skip == False:\n",
    "        #print(img.shape[0])\n",
    "        #print(i)\n",
    "        img=np.delete(img, i, 0)\n",
    "\n",
    "cv2.imwrite('3.3.1.png', img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'asdfg'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a='asdfg'\n",
    "a[0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 4]\n",
      " [2 5]\n",
      " [3 6]]\n",
      "6\n",
      "15\n"
     ]
    }
   ],
   "source": [
    "print(np.array([[1,2,3],[4,5,6]]).transpose())\n",
    "for arr in np.array([[1,2,3],[4,5,6]]):\n",
    "    print(sum(arr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  9,  9,  7],\n",
       "       [ 8,  9,  9, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=np.arange(16).reshape(4,4)#.shape\n",
    "x[1:3,1:3]=np.array([[9,9],[9,9]])\n",
    "x\n",
    "#print(sum(sum(x)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img = cv2.imread('3.3.png',0)\n",
    "\n",
    "iw = 0\n",
    "ih = 0\n",
    "h,w =img.shape\n",
    "while iw+3 <= w:\n",
    "    while ih+3 <= h:\n",
    "        #print(sum(sum(img[ih:ih+2, iw:iw+2])))\n",
    "        if np.count_nonzero(img[ih:ih+3, iw:iw+3]) > 4:\n",
    "            #print(ih, iw)\n",
    "            img[ih:ih+2, iw:iw+2] = 255\n",
    "        elif np.count_nonzero(img[ih:ih+3, iw:iw+3]) == -1:\n",
    "            #print(img[ih:ih+2, iw:iw+2])\n",
    "            img[ih:ih+2, iw:iw+2] = 0\n",
    "        ih += 3\n",
    "    ih = 0\n",
    "    iw += 3\n",
    "cv2.imwrite('3.3.2.png', img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[255, 255],\n",
       "       [255, 255]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([[255,255],[255,255]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.imread('3.3.png',0)\n",
    "elemmax = img.shape // 3 + 1\n",
    "while trust < 0.8:\n",
    "    elem = elemmax -1\n",
    "    sum(sum(img[ih:ih+2, iw:iw+2]))\n",
    "cv2.imwrite('3.3.3.png', img)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}